/*
 * 常用原生Javascript 函数
 *
 * @author sylthasgg@gmail.com
 * @version 1.0
 */
(function(window, undefined) {
    var ikits = {};



    ikits.bind = function(elem, type, fn) {
        var regexp = /^([\w\-]+)\.([\w\-]+)/,
            na = [null, type, null];
        !elem.events && (elem.events = {});

        var _types = type.match(regexp) || na,
            _clazz = _types[2] || (new Date().getTime()),
            _type = _types[1];

        if (!elem.events[_type]) elem.events[_type] = {};
        if (elem.events[_type][_clazz]) {
            var _events = elem.events[_type][_clazz];
            if (typeof _events == 'function')
                elem.events[_type][_clazz] = [_events];
            elem.events[_type][_clazz].push(fn);
        } else
            elem.events[_type][_clazz]  = fn;
        elem.addEventListener ? elem.addEventListener(_type, fn, false) : elem.attachEvent("on" + _type, fn);
    };

    ikits.unbind = function(elem, type, fn) {
        var regexp = /^([\w\-]+)\.([\w\-]+)/,
            na = [null, type, null];

        var _types = type.match(regexp) || na,
            _clazz = _types[2] || (new Date().getTime()),
            _type = _types[1];

        var handlers = elem.events[_type];
        var remove = function(elem, type, fn) {
            elem.removeEventListener ? elem.removeEventListener(type, fn, false) : elem.detachEvent("on" + type, fn);
        };

        if (fn) {
            for (_clazz in handlers) 
                if (handlers[_clazz] === fn) {
                    remove(elem, _type, fn);
                    delete handlers[_clazz];
                }
        } else {
            if (handlers[_clazz]) {
                if (handlers[_clazz].length > 1) {
                    var _handlers = handlers[_clazz];
                    for (var i in _handlers)
                        remove(elem, _type, _handlers[i]);
                } else 
                    remove(elem, _type, handlers[_clazz]);
            } else
                for (var i in handlers) 
                    remove(elem, _type, handlers[i]);
        }
    };
    //copy from jQ
    ikits.extend = function() {
        var target = arguments[0] || {}, i = 1,
            length = arguments.length,
            deep = true,
            options;
        if (typeof target === "boolean") {
            deep = target;
            target = arguments[1] || {};
            i = 2;
        }
        if (typeof target !== "object" && Object.prototype.toString.call(target) != "[object Function]")
            target = {};
        for (; i < length; i++) {
            if ((options = arguments[i]) != null)
                for (var name in options) {
                    var src = target[name],
                        copy = options[name];
                    if (target === copy)
                        continue;
                    if (deep && copy && typeof copy === "object" && !copy.nodeType) {
                        target[name] = arguments.callee(deep, src || (copy.length != null ? [] : {}), copy);
                    } else if (copy !== undefined)
                        target[name] = copy;
                }
        }
        return target;
    };
    //copy from jQ
    ikits.each = function(object, callback, args) {
        var name, i = 0,
            length = object.length;
        if (args) {
            args = Array.prototype.slice.call(arguments).slice(2);
            if (length === undefined) {
                for (name in object)
                    if (callback.apply(object[name], [name, object[name]].concat(args)) === false)
                        break;
            } else
                for (; i < length; i++)
                    if (callback.apply(object[i], [i, object[i]].concat(args)) === false) //
                        break;
        } else {
            if (length === undefined) {
                for (name in object)
                    if (callback.call(object[name], name, object[name]) === false)
                        break;
            } else
                for (var value = object[0]; i < length && callback.call(value, i, value) !== false; value = object[++i]) {}
        }
        return object;
    };

    window['ikits'] = ikits;
})(window);